rm(list = ls())


# Set Working Directory to Replication Folder
taskdir 		<- "~/Dropbox/final_hai_perlman_replication/"
inputdir 		<- paste0(taskdir, "input/processing/regressions_survey2/")


setwd(taskdir)
library(tidyverse)
library(lfe)
require(data.table)
library(stringi)
library(stargazer)
library(kableExtra)
library(modelsummary)



# Load regression results
load(paste0(inputdir, 'ols_survey2_nocontrols.RData'))


# We need this only for the main DVs
main_dvs <- c('politician_understand' ,
			  'politician_prevent' ,
			   'politician_advocate' ,
			   'politician_sympathy',
			   'tax_support')
main_dvs_labels <- c('Confidence that politician has a good understanding of wildfires and their causes?' ,
			  'Confidence that politician will work to prevent future wildfires?' ,
			   'Confidence that politician will be an effective advocate for federal disaster relief?' ,
			   'How sympathetic or unsympathetic did the politician seem towards those impacted?',
			   'How likely would you be to support this new tax?')


# No Controls Results Study 2 - tabular format
mods_study <- list()
for (i in 1:length(main_dvs)){
	dv <- main_dvs[i]
	temp_regressions <- outlist[[dv]]
	# extract models for a given dv
	mod1 <- temp_regressions[[1]]
	mod2 <- temp_regressions[[2]]
	mod3 <- temp_regressions[[3]]
	mod4 <- temp_regressions[[4]]
	mod5 <- temp_regressions[[5]]
	mod6 <- temp_regressions[[6]]
	mod7 <- temp_regressions[[7]]
	mod8 <- temp_regressions[[8]]

	mods_study[[i]] <- list(
		"(1)" = mod1, 
		"(2)" = mod2, 
		"(3)" = mod3, 
		"(4)" = mod4, 
		"(5)" = mod5, 
		"(6)" = mod6, 
		"(7)" = mod7, 
		"(8)" = mod8)
}

for (i in 1:length(main_dvs)) {
	dvlabel <-c(1, 8)
	names(dvlabel) <- c(" ",main_dvs_labels[i])
	# Use model summary to save model summary
	modout<- modelsummary(mods_study[[i]], 
			     output = "latex",
			     vcov = mods_study[[i]]%>%unname()%>%lapply(FUN = function(m)m$rse),
			     stars = c('*' =.1, '**' = .05, '***' = 0.01),
			     notes = 'Regressions also include no controls or fixed effects')%>%
	  add_header_above(c(" ", "Pooled" = 1, "Republicans" = 1, "Democrats" = 1, "Independents" = 1,
	  						  "Pooled" = 1, "Republicans" = 1, "Democrats" = 1, "Independents" = 1))%>%
	  add_header_above(c(" ", "Republican Politician" = 4, "Democratic Politician " = 4)) %>%
	  add_header_above(dvlabel)%>%
	  add_header_above(c(" ", "Dependent variable:"=8))
	# output to latex
	save_kable(
		modout,
		file = paste0(taskdir, 'output/regression_tables/ols_survey2_nocontrols/survey2_table_nocontrols_',i,'.tex'),
	)
	# manually remove \begin and \end table
	flname<- paste0(taskdir, 'output/regression_tables/ols_survey2_nocontrols/survey2_table_nocontrols_',i,'.tex')
	lines <- readLines(flname) # read file
	lines[1] <- ""           # I wanted to replace the first and last lines
	lines[length(lines)] <- ""
	write(lines,flname)
}





















